package com.macro.mall.tiny.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.common.api.ResultCode;
import com.macro.mall.tiny.modules.pms.model.PmsNavigation;
import com.macro.mall.tiny.modules.pms.service.PmsNavigationService;
import com.macro.mall.tiny.security.util.AdminHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 导航栏管理Controller
 */
@RestController
@Api(tags = "PmsNavigationController", description = "导航栏管理")
@RequestMapping("/pms/navigation")
public class PmsNavigationController {
    @Autowired
    private PmsNavigationService navigationService;
    
    @Autowired
    private AdminHolder adminHolder;

    @ApiOperation("获取所有显示的导航栏列表")
    @GetMapping("/listVisible")
    public CommonResult<List<String>> listVisible() {
        QueryWrapper<PmsNavigation> wrapper = new QueryWrapper<>();
        wrapper.eq("hidden", 1)
               .orderByAsc("sort")
               .select("name");
        List<PmsNavigation> navigationList = navigationService.list(wrapper);
        List<String> tabList = navigationList.stream()
                .map(PmsNavigation::getName)
                .collect(java.util.stream.Collectors.toList());
        // 在开头添加"全部"选项
        tabList.add(0, "全部");
        return CommonResult.success(tabList);
    }

    @ApiOperation("后台查询导航栏")
    @GetMapping("/list")
    public CommonResult<List<PmsNavigation>> list(@RequestParam(value = "keyword", required = false) String keyword) {
        if (!adminHolder.isAdmin()) {
            return CommonResult.failed(ResultCode.FORBIDDEN);
        }
        QueryWrapper<PmsNavigation> name = new QueryWrapper<PmsNavigation>();
        if (keyword != null && !keyword.isEmpty()) {
            name.like("name", keyword);
        }
        return CommonResult.success(navigationService.list(name));
    }

    @ApiOperation("添加导航栏")
    @PostMapping("/create")
    public CommonResult<?> create(@RequestBody PmsNavigation pmsNavigation) {
        if (!adminHolder.isAdmin()) {
            return CommonResult.forbidden("权限不足");
        }
        boolean success = navigationService.create(pmsNavigation);
        if (success) {
            return CommonResult.success(pmsNavigation);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改导航栏")
    @PostMapping("/update/{id}")
    public CommonResult<?> update(@PathVariable Long id, @RequestBody PmsNavigation pmsNavigation) {
        if (!adminHolder.isAdmin()) {
            return CommonResult.forbidden("权限不足");
        }
        boolean success = navigationService.update(id, pmsNavigation);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除导航栏")
    @PostMapping("/delete/{id}")
    public CommonResult<?> delete(@PathVariable Long id) {
        if (!adminHolder.isAdmin()) {
            return CommonResult.forbidden("权限不足");
        }
        boolean success = navigationService.delete(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量删除")
    @PostMapping("deleteAll")
    public CommonResult<?> deleteAll(@RequestBody List<Long> ids) {
        if (!adminHolder.isAdmin()) {
            return CommonResult.failed();
        }
        boolean b = navigationService.removeBatchByIds(ids);
        return CommonResult.success(b);
    }
}
